<?
/**
 * 쿠폰 (Coupon for Gnuboard4)
 *
 * Copyright (c) 2012 Choi Jae-Young <www.miwit.com>
 *
 * 저작권 안내
 * - 저작권자는 이 프로그램을 사용하므로서 발생하는 모든 문제에 대하여 책임을 지지 않습니다. 
 * - 이 프로그램을 어떠한 형태로든 재배포 및 공개하는 것을 허락하지 않습니다.
 * - 이 저작권 표시사항을 저작권자를 제외한 그 누구도 수정할 수 없습니다.
 */

include_once("_common.php");
include_once("../_config.php");
include_once("../_lib.php");

if ($is_admin != "super")
    alert_close("최고관리자만 접근할 수 있습니다.");

$coupon_info = sql_fetch("select * from $mw_coupon[info_table] where cp_id = '$cp_id'");
if (!$coupon_info)
    alert_close("데이터가 없습니다.");

$g4[title] = "$coupon_info[cp_subject] - 쿠폰 내역";
include_once("$g4[path]/head.sub.php");

$sql_common = " from $mw_coupon[list_table] ";
$sql_search = " where cp_id = '$cp_id' ";
$sql_order = " order by cp_use desc, cp_coupon ";

if ($sfl && $stx)
    $sql_search .= " and $sfl like '%{$stx}%' ";

$sql = "select count(*) as cnt
        $sql_common
        $sql_search";
$row = sql_fetch($sql);
$total_count = $row[cnt];

$rows = $config[cf_page_rows];
//$rows = 20;
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page == "") { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함  */

$sql = "select *
        $sql_common
        $sql_search
        $sql_order
        limit $from_record, $rows ";
$qry = sql_query($sql);

$list = array();
for ($i=0; $row=sql_fetch_array($qry); $i++) {
    $list[$i] = $row;
    //$list[$i][num] = $total_count - ($page - 1) * $rows - $i;
    $list[$i][num] = ($page - 1) * $rows + $i+1;

    $list[$i][url] = '';
    if ($row[mb_id]) {
        if ($coupon_info[cp_type] == 'p') {
            $list[$i][url] = "$g4[admin_path]/point_list.php?sfl=mb_id&stx={$row[mb_id]}";
        } else if ($coupon_info[cp_type] == 'c') {
            $list[$i][url] = "$g4[admin_path]/mw.cash/mw.cash.list.php?sfl=mb_id&stx={$row[mb_id]}";
        }
    }
}

$list_count = count($list);

$tmp = sql_fetch("select count(*) as cnt from $mw_coupon[list_table] where cp_id = '$cp_id' and cp_use = '1'");
$use_count = $tmp[cnt];

$write_pages = get_paging($rows, $page, $total_page, "$PHP_SELF?{$qstr}&cp_id=$cp_id&page=");
?>

<link rel="stylesheet" href="style.css" type="text/css"/>

<script type="text/javascript">
function send_memo(obj, coupon) {
    if (!confirm("쪽지를 발송하시겠습니까?")) return false;
    tmp = $(obj).html();
    $(obj).html("<img src='icon_loading.gif' align='absmiddle'>");
    $.post("send_memo.php", { 'cp_id':<?=$cp_id?>, 'cp_coupon':coupon }, function (ret) {
        alert(ret);
        $(obj).html(tmp);
    });
}
</script>

<script type="text/javascript" src="<?=$g4[path]?>/js/sideview.js"></script>

<div class="f">
    <div class="fp">
        <a href="./coupon_list.php?cp_id=<?=$cp_id?>"><?=$g4[title]?> <?="$use_count/$total_count"?></a>&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="./download.php?cp_id=<?=$cp_id?>&stx=<?=$stx?>" style="color:#ff0000;">[전체 다운로드]</a>&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="./download.php?cp_id=<?=$cp_id?>&stx=<?=$stx?>&use=1" style="color:#ff0000;">[미사용분 다운로드]</a>&nbsp;&nbsp;&nbsp;&nbsp;
        <a style="color:#008800; cursor:pointer;"  onclick="send_memo(this)">[지정회원ID 전체쪽지 발송]</a>
    </div>
    <div class="fb">
        <form name="fsearch" method="get" action="<?=$_SERVER[PHP_SELF]?>">
        <select name="sfl">
            <option value="cp_coupon"> 쿠폰번호 </option>
            <option value="mb_id"> 사용회원ID </option>
            <option value="mb_id_only"> 지정회원ID </option>
            <option value="cp_ip"> 사용IP </option>
        </select>
        <input type="hidden" name="cp_id" value="<?=$cp_id?>">
        <input type="text" name="stx" value="<?=$stx?>" class="ed" size="20" required>
        <input type="submit" class="b" value="검색">
        </form>
        <? if ($sfl) { ?> <script type="text/javascript"> fsearch.sfl.value = '<?=$sfl?>'; </script> <? } ?>
    </div>
</div>

<table border="0" cellpadding="0" cellspacing="0" width="100%" class="t">
<tr>
    <td class="tt" width=""> 순서 </td>
    <td class="tt" width=""> 쿠폰번호 </td>
    <td class="tt" width="100"> 사용 </td>
    <td class="tt" width="120"> 사용일시 </td>
    <td class="tt" width="120"> IP </td>
    <td class="tt" width="100"> 사용회원ID </td>
    <td class="tt" width="150"> 지정회원ID </td>
</tr>
<?
for ($i=0; $i<$list_count; $i++)
{
    $row = $list[$i];

    $row[name] = '-';
    $row[name_only] = '';

    if ($row[mb_id]) {
        $tmp = get_member($row[mb_id], "mb_id, mb_nick, mb_email, mb_homepage");
        $row[name] = get_sideview($tmp[mb_id], $tmp[mb_nick], $tmp[mb_email], $tmp[mb_homepage]);
    }

    if ($row[mb_id] != $row[mb_id_only]) {
        $tmp = get_member($row[mb_id_only], "mb_id, mb_nick, mb_email, mb_homepage");
        $row[name_only] = get_sideview($tmp[mb_id], $tmp[mb_nick], $tmp[mb_email], $tmp[mb_homepage]);
    } else {
        $row[name_only] = $row[name];
    }

    if ($row[name_only] == '-') $row[name_only] = '';

    if ($row[cp_use_datetime] == '0000-00-00 00:00:00') $row[cp_use_datetime] = '-';
    if ($row[cp_ip] == '') $row[cp_ip] = '-';
    if ($row[mb_id] == '')
        $row[mb_id] = '-';
    else
        $row[mb_id] = "<a href='$row[url]' target='_blank'>$row[mb_id]</a>";
?>
<tr>
    <td class="tls"> <?=$row[num]?> </td>
    <td class="tls" style="font:normal 12px gulimche;"> <?=$row[cp_coupon]?> </td>
    <td class="tls"> <? if ($row[cp_use]) echo "√"; else echo "-"; ?> </td>
    <td class="tls"> <?=$row[cp_use_datetime]?> </td>
    <td class="tls"> <?=$row[cp_ip]?> </td>
    <td class="tls"> <?=$row[name]?> </td>
    <td class="tls" style="text-align:right;">
        <?=$row[name_only]?>
        <? if ($row[mb_id_only]) { ?>
        &nbsp;<a style="color:#999; font:normal 11px gulim; cursor:pointer;" onclick="send_memo(this, '<?=$row[cp_coupon]?>')">[쪽지발송]</a>
        <? } else {?>
        <div style="text-align:center;">-</div>
        <? } ?>
    </td>
</tr>
<? } ?>

<? if (!$i) { ?>
<tr>
    <td class="tn" colspan="7"> 등록된 쿠폰이 없습니다. </td>
</tr>
<? } ?>
</table>

<div style="text-align:center; padding:30px 0 50px 0;"><?=$write_pages?></div>

<?
include_once("$g4[path]/tail.sub.php");
?>
